﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using WxSDK.Common;
using WxSDK.Model;
using System.Web;

namespace WxSDK.CacheSys
{
    public static class WeChatMemory
    {
        public static WxCarroit.WxSdkEvan Evan = new WxCarroit.WxSdkEvan();
        /// <summary>
        /// WebConfig 设置Cache过期时间
        /// </summary>
        public static int CacheTimeOut = 36000; //int.Parse(WeChatCommon.getWebConfig("CacheTimeOut"));
        /// <summary>
        /// 微信获取用户信息页面的回调 , 如果用户信息
        /// </summary>
        public static string WxUserInfoRedirect = "/wx.aspx"; // WeChatCommon.getWebConfig("WxUserInfoRedirect");

        ///// <summary>
        ///// 缓存每个用户的Config配置文件
        ///// </summary>
        ///// <param name="MemberId"></param>
        ///// <returns></returns>
        //public static wx_member MemberConfig(int MemberId)
        //{
        //    var config = WeChatCache.GetValue("MemberConfig" + MemberId);
        //    if (config == null)
        //    {
        //        config = Evan.GetModel<WxSDK.Model.wx_member>(MemberId);
        //        WeChatCache.Insert_NoSliding("MemberConfig" + MemberId, config, CacheTimeOut);
        //    }
        //    return (wx_member)config;
        //}

        /// <summary>
        /// 将每个用户的自动回复缓存起来 不用每次都去数据库调取
        /// </summary>
        /// <param name="MemberId">微信公众号ID</param>
        /// <param name="IsStudy">是否开启学习功能</param>
        /// <returns></returns>
        public static List<wx_replay> ReplayMsg(int MemberId, int IsStudy)
        {
            var replaymsg = WeChatCache.GetValue("ReplayMsg" + MemberId);
            if (replaymsg == null)
            {
                replaymsg = Evan.Query<wx_replay>(3000, string.Format("MemberId = {0} and islock=1 {1}", MemberId, (IsStudy == 1 ? "" : "and IsStudy=0")), "Rank asc,AddTime desc").ToList();
                WeChatCache.Insert_NoSliding("ReplayMsg" + MemberId, replaymsg, CacheTimeOut);
            }
            return (List<wx_replay>)replaymsg;
        }

        /// <summary>
        /// 获取公众号信息
        /// </summary>
        /// <param name="MemberId"></param>
        /// <returns></returns>
        public static Model.wx_member MemberInfo(int MemberId)
        {
            var member = WeChatCache.GetValue("Member" + MemberId);
            if (member == null)
            {
                member = Evan.GetModel<WxSDK.Model.wx_member>(MemberId);
                WeChatCache.Insert_NoSliding("Member" + MemberId, member, CacheTimeOut);
            }
            return (wx_member)member;
        }

        /// <summary>
        /// 获取全局AccessToken
        /// </summary>
        /// <param name="MemberId">公众号ID</param>
        /// <returns></returns>
        public static string AccessToken(int MemberId)
        {
            return WeChatApi.GetAccessToken(MemberId);
        }

        /// <summary>
        /// 获取全局JS票据
        /// </summary>
        /// <param name="MemberId"></param>
        /// <returns></returns>
        public static string GetJsApi_Ticket(int MemberId)
        {
            return WeChatApi.GetJsApiTicket(MemberId);
        }

        /// <summary>
        /// 获取当前登录的微信用户信息
        /// </summary>
        /// <returns></returns>
        public static WeChatModel.userInfo GetCurWxUserInfo()
        {
            if (WeChatCache.GetValue(CommFun.WeChatSessionIdentifierGet() + "_wx_user") == null)
            {
                //WeChat.WeChatCommon.WeChatLog(WxUserInfoRedirect + "?from=" + HttpContext.Current.Request.RawUrl);
                //throw new Exception(WeChatConf.MemberId().ToString());
                HttpContext.Current.Response.Redirect(WxUserInfoRedirect + "?from=" + WxSDK.Common.CommFun.UrlEncode(HttpContext.Current.Request.RawUrl) + "&mid=" + WeChatConf.MemberId());
            }
            return (WeChatModel.userInfo)WeChatCache.GetValue(CommFun.WeChatSessionIdentifierGet() + "_wx_user");
        }

    }
}
